package sjm.parse;

import java.util.*;
import java.util.stream.Collectors;
import lombok.Getter;
/*
 * Copyright (c) 1999 Steven J. Metsker. All Rights Reserved.
 * 
 * Steve Metsker makes no representations or warranties about
 * the fitness of this software for any particular purpose, 
 * including the implied warranty of merchantability.
 */

/**
 * This class abstracts the behavior common to parsers that consist of a series
 * of other parsers.
 *
 * @author Steven J. Metsker
 *
 * @version 1.0
 */
public abstract class CollectionParser extends Parser {

    /**
     * this parser's sub-parsers.
     */
    @Getter
    protected List<Parser> subparsers = new ArrayList();

    /**
     * Supports subclass constructors with no arguments.
     */
    public CollectionParser() {
    }

    /**
     * Supports subclass constructors with a name argument
     *
     * @param name the name of this parser
     */
    public CollectionParser(String name) {
        super(name);
    }

    /**
     * Adds a parser to the collection.
     *
     * @param parser the parser to add
     *
     * @return this
     */
    public CollectionParser add(Parser parser) {
        subparsers.add(parser);
        return this;
    }

    /**
     * Helps to textually describe this CollectionParser.
     *
     * @return the string to place between parsers in the collection
     */
    protected abstract String toStringSeparator();

    /**
     * @return a textual description of this parser.
     */
    @Override
    protected String unvisitedString(List<Parser> visited) {
        String des = visited.stream()
                .map(p -> p.toString(visited))
                .collect(Collectors.joining(toStringSeparator()));
        return "<" + des + ">";
    }
}
